package com.bindatax.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class PropertiesUtil {

    public static Properties read(String configPath){
        Properties properties = new Properties();
        if(configPath == null){
            return properties;
        }
        try (InputStream inputStream = Files.newInputStream(Paths.get(configPath))) {
            properties.load(inputStream);
        } catch (IOException e) {
            System.out.println("获取配置文件["+configPath+"]内容失败：" + StringUtils.getExceptionInfo(e));
        }
        return properties;
    }


    public static String get(Properties properties,String key,String defaultValue){
        String value = properties.getProperty(key);
        if(value == null || value.length() == 0){
            return defaultValue;
        }
        return value;
    }
    public static int get(Properties properties,String key,int defaultValue){
        String value = properties.getProperty(key);
        if(value == null || value.length() == 0){
            return defaultValue;
        }
        return Integer.parseInt(value);
    }
    public static long get(Properties properties,String key,long defaultValue){
        String value = properties.getProperty(key);
        if(value == null || value.length() == 0){
            return defaultValue;
        }
        return Long.parseLong(value);
    }

    public static boolean get(Properties properties,String key,boolean defaultValue){
        String value = properties.getProperty(key);
        if(value == null || value.length() == 0){
            return defaultValue;
        }
        return "y".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
    }

    public static double get(Properties properties,String key,double defaultValue){
        String value = properties.getProperty(key);
        if(value == null || value.length() == 0){
            return defaultValue;
        }
        return Double.parseDouble(value);
    }

}
